﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;
using t01crm.Data;

namespace t01crm.View
{
    public class SalesListViewModel : ViewModelBase
    {
        public ObservableCollection<HG_Sell> SalesListData { get; private set; }
        public ICommand SearchCommand { get; private set; }
        public ICommand SaveCommand { get; private set; }
        public SalesListViewModel()
        {
            SalesListData = new ObservableCollection<HG_Sell>();
            StartedDateFrom = DateTime.Today.AddDays(-7);
            StartedDateEnd = DateTime.Today;
            SearchCommand = new DelegateCommand<object>(o =>
            {
                LoadSalesList();
            });
            SaveCommand = new DelegateCommand<object>(o =>
            {
                SaveSell();
            });
        }

        private void SaveSell()
        {
            using (tb01crmEntities ctx = new tb01crmEntities())
            {
               
                HG_Ware hw = new HG_Ware();
                hw.ware_name = "hi";
              
                ctx.AddToHG_Ware(hw);
               
                ctx.SaveChanges();
            }
        }
        private DateTime startedDateFrom;
        public DateTime StartedDateFrom
        {
            get
            {
                return startedDateFrom;
            }
            set
            {
                if (startedDateFrom != value)
                {
                    startedDateFrom = value;
                    FirePropertyChanged("StartedDateFrom");
                }
            }
        }
        private HG_Sell currentHG_Sell;
        public HG_Sell CurrentHG_Sell
        {
            get
            {
                return currentHG_Sell;
            }
            set
            {
                if (currentHG_Sell != value)
                {
                    currentHG_Sell = value;
                    FirePropertyChanged("CurrentHG_Sell");
                }
            }
        }
        private DateTime startedDateEnd;
        public DateTime StartedDateEnd
        {
            get
            {
                return startedDateEnd;
            }
            set
            {
                if (startedDateEnd != value)
                {
                    startedDateEnd = value;
                    FirePropertyChanged("StartedDateEnd");
                }
            }
        }
        void LoadSalesList()
        {

            SalesListData.Clear();


            using (tb01crmEntities ctx = new tb01crmEntities())
                {


                    List<HG_Sell> hgsell = (from c in ctx.HG_Sell 
                                            where c.sell_crtdate<=StartedDateEnd && c.sell_crtdate>=StartedDateFrom 
                                            
                                            select c).ToList();
                    foreach (HG_Sell hgs in hgsell)
                    {
                        SalesListData.Add(hgs);
                    }   
                }
          
          
                   
         }
    }
}
